package cn.zy.register.controller;

import cn.zy.common.alisms.dto.AUTHCode;
import cn.zy.common.encryption.AuthRandomUtils;
import cn.zy.common.redis.JacksonUtil;
import cn.zy.common.redis.RedisServie;
import cn.zy.common.util.BeanCopierUtil;
import cn.zy.common.utils.RestResult;
import cn.zy.common.utils.RestResultEmum;
import cn.zy.init.DataConstant;
import cn.zy.register.mapper.bo.SmallMiUserBO;
import cn.zy.register.mapper.domain.SmallMiUserDO;
import cn.zy.register.mapper.dto.SmallMiUserDTO;
import cn.zy.register.mapper.pojo.SmallMiUser;
import cn.zy.register.mapper.vo.SmallMiUserVO;
import cn.zy.register.service.SmallMiUserService;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/login")
public class SmallMiUserController {

    /**
     * 发送短信
     */
    @Autowired
    private Client client;

    /**
     * 用户 service
     */
    @Autowired
    private SmallMiUserService smallMiUserService;


    /**
     * 查询全部用户信息
     * /login/getAll
     *
     * @return
     */
    @GetMapping("/getAll")
    public List<SmallMiUser> getAll() {
        return smallMiUserService.getAll();
    }


    /**
     * 获取验证码
     * /login/verify
     *
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping("/verify")
    public RestResult<String> getCode(String miPhone) {
        RestResult<String> getcode = smallMiUserService.getcode(miPhone);
        String data = getcode.getData();
        return new RestResult<>(getcode.getCode(), getcode.getMsg(), data);
    }


    /**
     * 注册验证
     * /login/register
     *
     * @param mi_phone
     * @param code
     * @param agreement
     * @return
     */
    @PostMapping("/register")
    public RestResult<String> register(SmallMiUserDTO mi_phone, String code, String agreement) {
        SmallMiUserBO smallMiUserBO = BeanCopierUtil.copyObj(mi_phone, SmallMiUserBO.class);
        RestResult<SmallMiUserDO> register = smallMiUserService.register(smallMiUserBO, code, agreement);
        SmallMiUserDO data = register.getData();
        if (data == null) {
            return new RestResult<>(register.getCode(), register.getMsg());
        }
        String mi_phone1 = data.getMi_phone();
        return new RestResult<>(register.getCode(), register.getMsg(), mi_phone1);
    }


    /**
     * /login/verifyLogin
     * 已注册登录  判断密码是否正确
     *
     * @param mi_phone
     * @param mi_pass_word
     * @return
     */
    @PostMapping("/verifyLogin")
    public RestResult<SmallMiUserVO> verifyLogin(String mi_phone, SmallMiUserDTO mi_pass_word) {

        SmallMiUserBO smallMiUserBO = BeanCopierUtil.copyObj(mi_pass_word, SmallMiUserBO.class);
        RestResult<SmallMiUserDO> smallMiUserDORestResult = smallMiUserService.verifyLogin(mi_phone, smallMiUserBO);
        SmallMiUserDO data = smallMiUserDORestResult.getData();
        if (data != null) {
            SmallMiUserVO smallMiUserVO = BeanCopierUtil.copyObj(data, SmallMiUserVO.class);
            return new RestResult<>(smallMiUserDORestResult.getCode(), smallMiUserDORestResult.getMsg(), smallMiUserVO);
        }

        return new RestResult<>(smallMiUserDORestResult.getCode(), smallMiUserDORestResult.getMsg());
    }


    /**
     * /login/setMiAccount
     * 输入密码 生成小米账号
     *
     * @return
     */
    @PostMapping("/setMiAccount")
    public RestResult<SmallMiUserVO> setMiAccount(SmallMiUserDTO smallMiUserDTO) {
        SmallMiUserBO smallMiUserBO = BeanCopierUtil.copyObj(smallMiUserDTO, SmallMiUserBO.class);
        RestResult<SmallMiUserDO> smallMiUserDO = smallMiUserService.setMiAccount(smallMiUserBO);
        SmallMiUserDO data = smallMiUserDO.getData();
        if (data != null) {
            SmallMiUserVO smallMiUserVO = BeanCopierUtil.copyObj(data, SmallMiUserVO.class);
            return new RestResult<>(smallMiUserDO.getCode(), smallMiUserDO.getMsg(), smallMiUserVO);
        }
        return new RestResult<>(smallMiUserDO.getCode(), smallMiUserDO.getMsg());
    }


}
